//
//  NewRegisterViewController.m
//  FD
//
//  Created by xiaofang wang on 2018/11/5.
//  Copyright © 2018年 ZhiYuan Network. All rights reserved.
// 新注册VC

//1-20
#define kDefalultTFViewFrame CGRectMake(0, 0, SCREEN_WIDTH, 50)

#import "NewRegisterViewController.h"
#import "FDSameToAndroidTextFieldCustomView.h" //2019-1-20
//#import "RBTextField.h"
#import "FDSendVerifyCodeButton.h"
@interface NewRegisterViewController ()
/**1.电话 2019-2-23 不要了*/
//@property (nonatomic, strong) FDSameToAndroidTextFieldCustomView *phoneTFView;
/**2.邮箱*/
@property (nonatomic, strong) FDSameToAndroidTextFieldCustomView *emailTFView;
/**3.邮箱验证码*/
@property (nonatomic, strong) FDSameToAndroidTextFieldCustomView *verifyCodeTFView;
/**4.密码 默认密文显示 1-19*/
@property (nonatomic, strong) FDSameToAndroidTextFieldCustomView *pwdTFView;
/**5.确认登录密码 默认密文显示 1-19*/
@property (nonatomic, strong) FDSameToAndroidTextFieldCustomView *surepwdTFView;
/**6.注册按钮*/
@property (nonatomic, strong) UIButton *registerBtn;
/**7.获取要验证码返回的token*/
@property (nonatomic, copy) NSString *token;

//注册键盘遮盖问题
@end

@implementation NewRegisterViewController
#pragma mark - view生命周期
- (void)viewDidLoad {
    [super viewDidLoad];

    [self setupView];
    
    [self __localInternationalization]; //国际化
}

- (void)setupView {
    
    [self __setupNav];
    [self __setupChildView];
}

- (void)__setupNav {
    NSString *registerStr = NSLocalizedString(Register, nil); //注册
    self.navigationItem.title = registerStr;
    self.view.backgroundColor = [UIColor whiteColor];

    //添加右侧返回按钮
    UIBarButtonItem *barItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"返回箭头.png"] style:(UIBarButtonItemStylePlain) target:self action:@selector(backAction:)];
    barItem.tintColor = [UIColor blackColor];
    self.navigationItem.leftBarButtonItem = barItem;
}

- (void)backAction:(UIBarButtonItem *)barButtonItem {
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)__setupChildView {
    
    //5个输入框相关的view都添加在这上面。 upBgView添加在self.view上
    UIView *upBgView = [[UIView alloc] init];
    
    [self.view addSubview:upBgView];
//    [upBgView addSubview:self.phoneTFView];//0.账号 2019-2-23 不要了
    [upBgView addSubview:self.emailTFView];//1.邮箱
    [upBgView addSubview:self.verifyCodeTFView];//2.邮箱验证码
    [upBgView addSubview:self.pwdTFView]; //3.登录密码
    [upBgView addSubview:self.surepwdTFView]; //4.确认密码
    
    
    [upBgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view);
        CGFloat h = 50 * 5 + 10*4; //50*5+ 10(间距)*4
        make.height.mas_equalTo(h);
        make.top.equalTo(self.view.mas_top).offset(FD_NavH+15);
    }];
    
    
    NSMutableArray *tempViewArr = [NSMutableArray array];
//    [tempViewArr addObject:self.phoneTFView]; //2019-2-23 不要了
    [tempViewArr addObject:self.emailTFView];
    [tempViewArr addObject:self.verifyCodeTFView];
    [tempViewArr addObject:self.pwdTFView];
    [tempViewArr addObject:self.surepwdTFView];
    [tempViewArr mas_distributeViewsAlongAxis:MASAxisTypeVertical withFixedSpacing:10 leadSpacing:0 tailSpacing:0];
    [tempViewArr mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left);
        make.right.equalTo(self.view.mas_right);
    }];
    
    
    
    
    //注册按钮
    [self.view addSubview:self.registerBtn];
    [self.registerBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left).offset(15);
        make.right.equalTo(self.view.mas_right).offset(-15);
        make.height.mas_equalTo(44);
        make.top.equalTo(upBgView.mas_bottom).offset(26);
    }];
}




#pragma mark - getter
//2019-2-23 不要了
//- (FDSameToAndroidTextFieldCustomView *)phoneTFView {
//    if (!_phoneTFView) {
//        NSString *zstr = NSLocalizedString(ToastPutAccount, @"请输入账号");
//
//        _phoneTFView = [[FDSameToAndroidTextFieldCustomView alloc] initWithFrame:kDefalultTFViewFrame title:zstr];
//    }
//    return _phoneTFView;
//}

- (FDSameToAndroidTextFieldCustomView *)emailTFView {
    if (!_emailTFView) {
        _emailTFView = [[FDSameToAndroidTextFieldCustomView alloc] initWithFrame:kDefalultTFViewFrame title:NSLocalizedString(Put_your_email, @"请输入邮箱")];
        
        //添加右侧按钮发送验证码
        FDSendVerifyCodeButton *btn = [[FDSendVerifyCodeButton alloc] initWithFrame:CGRectMake(0, 0, 100, 40)];
        [btn addTarget:self action:@selector(clickSendCodeAction:) forControlEvents:(UIControlEventTouchUpInside)];
        [_emailTFView.topBgView addSubview:btn];
        [btn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.right.bottom.equalTo(self->_emailTFView.topBgView);
            make.width.mas_greaterThanOrEqualTo(100);
        }];
    }
    return _emailTFView;
}
- (FDSameToAndroidTextFieldCustomView *)verifyCodeTFView {
    if (!_verifyCodeTFView) {
        NSString *yxyzmStr = NSLocalizedString(ToastPutEmailCode, @"请输入邮箱验证码");
        _verifyCodeTFView = [[FDSameToAndroidTextFieldCustomView alloc] initWithFrame:kDefalultTFViewFrame title:yxyzmStr];
    }
    return _verifyCodeTFView;
}
- (FDSameToAndroidTextFieldCustomView *)pwdTFView {
    if (!_pwdTFView) {
        NSString *qszdlmmStr = NSLocalizedString(ToastPutLoginPwd, @"请设置登录密码");
        _pwdTFView = [[FDSameToAndroidTextFieldCustomView alloc] initWithFrame:kDefalultTFViewFrame title:qszdlmmStr];
        _pwdTFView.tf.secureTextEntry = YES; //默认密文
        //睁眼闭眼按钮
        //右边眼睛按钮
        //输入框右边睁眼闭眼按钮
        UIButton *openCloseEyeButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
        [openCloseEyeButton setImage:[UIImage imageNamed:@"by"] forState:(UIControlStateNormal)];
        [openCloseEyeButton setImage:[UIImage imageNamed:@"zy"] forState:(UIControlStateSelected)];
        [openCloseEyeButton addTarget:self action:@selector(clickOpenCloseEyeBtnAction:) forControlEvents:(UIControlEventTouchUpInside)];
        [_pwdTFView.topBgView addSubview:openCloseEyeButton];
        [openCloseEyeButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.right.bottom.equalTo(self->_pwdTFView.topBgView);
            make.width.mas_equalTo(30);
        }];

        
    }
    return _pwdTFView;
}

//睁眼闭眼
- (void)clickOpenCloseEyeBtnAction:(UIButton *)button {
    button.selected = !button.selected;
    
    if (button.selected) {
        self.pwdTFView.tf.secureTextEntry = NO;
        self.surepwdTFView.tf.secureTextEntry = NO;
    }else {
        self.pwdTFView.tf.secureTextEntry = YES;
        self.surepwdTFView.tf.secureTextEntry = YES;
    }
}

- (FDSameToAndroidTextFieldCustomView *)surepwdTFView {
    if (!_surepwdTFView) {
        NSString *qrdlmmStr = NSLocalizedString(ToastPutSurePwd, @"请确认登录密码");
        _surepwdTFView = [[FDSameToAndroidTextFieldCustomView alloc] initWithFrame:kDefalultTFViewFrame title:qrdlmmStr];
        _surepwdTFView.tf.secureTextEntry = YES; //默认密文
    }
    return _surepwdTFView;
}

- (UIButton *)registerBtn {
    if (!_registerBtn) {
        
        NSString *registerStr = NSLocalizedString(Register, nil); //国际化
        _registerBtn = [UIButton createFastRedBtnRadius:5 target:self action:@selector(clickRegisterAction:) title:registerStr];
        _registerBtn.layer.masksToBounds = YES;
        _registerBtn.layer.cornerRadius = 4; //弧度4
    }
    return _registerBtn;
}

#pragma mark - 国际化
- (void)__localInternationalization {
//    self.phoneTF.placeholder = NSLocalizedString(Put_your_phone, nil); //请输入手机号
//    self.emailTF.placeholder = NSLocalizedString(Put_your_email, nil); //请输入邮箱
//    self.verifyCodeTF.placeholder = NSLocalizedString(Put_your_verifyCode, nil); //请输入验证码
//    self.pwdTF.placeholder = NSLocalizedString(Put_your_pwd, nil); //请输入密码
//    self.surepwdTF.placeholder = NSLocalizedString(Put_your_surepwd, nil);//请输入确认密码
//    self.emailTF.placeholder = NSLocalizedString(Put_your_email, @"请输入邮箱"); //请输入邮箱
}

#pragma mark - 点击事件

#pragma mark - clickRegisterAction
//!!!!:1.发送验证码
- (void)clickSendCodeAction:(FDSendVerifyCodeButton *)btn
{
    FDLog(@"发送验证码");
    NSString *email = self.emailTFView.tf.text;
    if ([NSString isEmptyString:email]) {

        [JRToast showWithText:NSLocalizedString(Put_your_email, @"请输入邮箱")];
        return;
    }
    
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    
    [FDHttpAPi requestRegisterEmailVerifyCodeWithEmial:email successBlock:^(id responseObjc) {
        FDLog(@"注册:%@",responseObjc);
        
        [hud hideAnimated:YES];
        
        NSString *event = responseObjc[@"event"];
        if ([event isEqualToString:SUCCESS]) {
            [JRToast showWithText:NSLocalizedString(ToastVerifyCodeAlreadySent, @"验证码已发送")];
            //注册会有一个token
            NSString *token = responseObjc[@"data"][@"token"];
            
            self.token = token ? : @""; //2019-2-15防止为空时导致奔溃。
            
            [btn verifyCodeCountDownTime:60]; //请求成功在 开启倒计时

            //528c2551fe914324bafaf29a5bd78d39
        }else {
            [JRToast showWithText:responseObjc[describe]];
        }
    } failBlock:^(NSError *error) {
        FDLog(@"%@",[error localizedDescription]);
        
        [hud hideAnimated:YES];
    }];
}

- (void)clickRegisterAction:(UIButton *)btn
{
    //一个一个判断
//    NSString *phone = self.phoneTFView.tf.text; //2019-2-23 不要了
    NSString *email = self.emailTFView.tf.text;
    NSString *verifycode = self.verifyCodeTFView.tf.text;
    NSString *pwd = self.pwdTFView.tf.text;
    NSString *surePwd = self.surepwdTFView.tf.text;
    
    BOOL isValid = [self isValidPhone:@"" email:email code:verifycode pwd:pwd surePwd:surePwd];
    
    
    if (isValid) { //调接口
        NSDictionary *parDic = @{@"name":@"",
                                 @"password":pwd,
                                 @"againPassword":surePwd,
                                 @"email":email,
                                 @"code":verifycode,
                                 @"token":self.token
                                     };
        
        FDLog(@"注册请求参数:%@",parDic);
        [FDHttpAPi requestRegisterWithParDic:parDic successBlock:^(id responseObjc) {
            NSString *even  = responseObjc[@"event"];
            FDLog(@"注册:%@",responseObjc);
            if ([even isEqualToString:SUCCESS]) {
                [JRToast showWithText:@"注册成功"];
                //直接消失，让他到登录页面
                [self dismissViewControllerAnimated:YES completion:nil];

            }else {
                [JRToast showWithText:responseObjc[describe]];
            }
        } failureBlock:^(NSError *error) {
            FDLog(@"%@",[error localizedDescription]);
        }];
    }
}

- (BOOL)isValidPhone:(NSString *)phone email:(NSString *)email code:(NSString *)code pwd:(NSString *)pwd surePwd:(NSString *)surepwd{

    //2019-2-23不要了
//    if ([NSString isEmptyString:phone]) {
//        [JRToast showWithText:NSLocalizedString(ToastPutAccount, @"请输入手机号")];
//        return NO;
//    }
    
    if ([NSString isEmptyString:email]) {
        [JRToast showWithText:NSLocalizedString(Put_your_email, @"请输入邮箱")];
        return  NO;
    }
    if ([NSString isEmptyString:code]) {
        [JRToast showWithText:NSLocalizedString(Put_your_verifyCode, @"请输入验证码")];
        return  NO;
    }
    if ([NSString isEmptyString:pwd]) {
        NSString *qsrmmStr = NSLocalizedString(Put_your_pwd, @"请输入密码");
        [JRToast showWithText:qsrmmStr];
        return  NO;
    }
    if ([NSString isEmptyString:surepwd]) {
        
        [JRToast showWithText:NSLocalizedString(Put_your_surepwd, @"请输入确认密码")];
        return  NO;
    }
    
    //走到这一步，调接口注册。
     return YES;
}
@end
